package com.yangle.frame.admin.service;

import com.yangle.frame.admin.entity.FrameCodeItem;
import com.yangle.frame.admin.mapper.FrameCodeItemMapper;
import com.yangle.frame.common.service.BaseService;
import com.yangle.frame.common.utils.EhcacheUtils;
import com.yangle.frame.common.utils.MapperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;

/**
* Created by yangle on 2018/5/27.
*/
@Service
public class FrameCodeItemService implements BaseService<FrameCodeItem> {
@Autowired
private FrameCodeItemMapper frameCodeItemMapper;
@Autowired
private EhcacheUtils ehcacheUtils;
public List<FrameCodeItem> getAll(FrameCodeItem frameCodeItem) {
Example example = MapperUtils.getExampleByClass(FrameCodeItem.class, frameCodeItem);
return frameCodeItemMapper.selectByExample(example);
}

public void create(FrameCodeItem frameCodeItem) {
frameCodeItemMapper.insert(frameCodeItem);
    FrameCodeItem temp=new FrameCodeItem();
    temp.setCodeId(frameCodeItem.getCodeId());
    List<FrameCodeItem> codeItems = getAll(temp);
    ehcacheUtils.update(frameCodeItem.getCode(),codeItems);
}

public void update(FrameCodeItem frameCodeItem) {
frameCodeItemMapper.updateByPrimaryKey(frameCodeItem);
    FrameCodeItem temp=new FrameCodeItem();
    temp.setCodeId(frameCodeItem.getCodeId());
    List<FrameCodeItem> codeItems = getAll(temp);
    ehcacheUtils.update(frameCodeItem.getCode(),codeItems);
}

public FrameCodeItem select(String id) {
return frameCodeItemMapper.selectByPrimaryKey(id);
}

public void delete(String ids) {
    FrameCodeItem codeItem = select(Arrays.asList(ids.split(",")).get(0));
    frameCodeItemMapper.deletes(Arrays.asList(ids.split(",")));
    FrameCodeItem temp=new FrameCodeItem();
    temp.setCodeId(codeItem.getCodeId());
    List<FrameCodeItem> codeItems = getAll(temp);
    ehcacheUtils.update(codeItem.getCode(),codeItems);
}
}
